package org.springblade.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.oa.entity.InvoiceImage;
import org.springblade.oa.service.IInvoiceImageService;
import org.springblade.oa.vo.InvoiceImageVo;
import org.springblade.oa.wrappper.InvoiceImageWrapper;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author LK
 * @version 1.0.0
 * @Title: InvoiceImageController.java
 * @Description: TODO    发票图片控制器
 * @date 2020/4/15 11:02
 */
@RestController
@RequestMapping(value = "/invoiceImage")
@AllArgsConstructor
public class InvoiceImageController {

	private IInvoiceImageService imageService;
	private IUserClient userClient;

	/**
	 * 保存发票图片
	 *
	 * @param file
	 * @return
	 */
	@PostMapping("/save")
	public R save(@RequestParam("file") MultipartFile file, BladeUser bladeUser) {
		if (!file.isEmpty()) {
			String orgFilename = file.getOriginalFilename();
			InvoiceImage invoiceImage = new InvoiceImage();
			//文件后缀名（文件类型）
			String fileName = orgFilename.substring(0, orgFilename.lastIndexOf("."));
			String suffix = orgFilename.substring(orgFilename.lastIndexOf("."));
			//文件类型判断逻辑
			String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
//		File dest = new File("d:/oa/invoiceImage" + uuid + suffix);
//		File saveDir = new File("d:/oa/invoiceImage");
			String osName = System.getProperty("os.name").toLowerCase();
			File dest = new File("/home/oa/invoiceImage/" + uuid + suffix);
			File saveDir = new File("/home/oa/invoiceImage/");
			if (osName.indexOf("windows") >= 0) {
				dest = new File("D:\\oa\\invoiceImage\\" + uuid + suffix);
				saveDir = new File("D:\\oa\\invoiceImage");
			}
			if (!saveDir.exists() && !saveDir.isDirectory()) {
				saveDir.mkdirs();
			}
			try {
				file.transferTo(dest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			file.getContentType();
			invoiceImage.setOriginalName(fileName);
			invoiceImage.setName(uuid);
			invoiceImage.setType(suffix);
			invoiceImage.setCreateUser(bladeUser.getUserId());
			invoiceImage.setUpdateUser(bladeUser.getUserId());
			return R.status(true);
		}
		return R.status(false);
	}

	/**
	 * 批量上传图片
	 *
	 * @param files
	 * @param invoiceImage
	 * @return
	 */
	@PostMapping("/uploadImages")
	public R saveImages(@RequestParam("files") MultipartFile[] files, InvoiceImage invoiceImage,BladeUser bladeUser) {
		invoiceImage.setModelType("1");
		invoiceImage.setCreateUser(bladeUser.getUserId());
		invoiceImage.setUpdateUser(bladeUser.getUserId());
		invoiceImage.setCreateTime(new Date(System.currentTimeMillis()));
		invoiceImage.setUpdateTime(new Date(System.currentTimeMillis()));
		return R.status(imageService.saveBatch(files, invoiceImage));
	}

	/**
	 * 批量删除图片
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/remove")
	public R deleteImages(@RequestParam String ids) {
		return R.status(imageService.removeBatchByIds(ids));
	}

	/**
	 * 文件下载
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/downloadImage")
	public void downloadImage(HttpServletResponse response, @RequestParam String id) {
		imageService.downloadImage(response, id);
	}

	/**
	 * 图片预览
	 *
	 * @param response
	 * @param id
	 */
	@GetMapping(value = "preViewImage")
	public void preView(HttpServletResponse response, String id) {
		imageService.preView(response, id);
	}

	/**
	 * 批量下载
	 *
	 * @param ids
	 */
	@GetMapping(value = "/downloadBatch")
	public void downloadBatch(HttpServletResponse response, @RequestParam String ids,@RequestParam String zipName) {
		imageService.downloadBatch(response, ids,zipName);
	}

	/**
	 * 获取发票图片列表
	 *
	 * @param invoiceImage
	 * @return
	 */
	@GetMapping(value = "/InvoiceImageList")
	public R<List<InvoiceImageVo>> getInvoiceImageList(InvoiceImage invoiceImage) {
		return R.data(InvoiceImageWrapper.build().entityVOList(imageService.list(Wrappers.query(invoiceImage))));
	}

	/**
	 * 批量上传发票
	 *
	 * @param files
	 * @param invoiceImage
	 * @return
	 */
	@PostMapping("/uploadInvoicePdf")
	public R uploadInvoicePdf(@RequestParam("files") MultipartFile[] files, InvoiceImage invoiceImage,BladeUser bladeUser) {
		R<UserInfo> userInfo = userClient.userInfo(bladeUser.getUserId().longValue());
		invoiceImage.setModelType("1");
		invoiceImage.setCreateUser(bladeUser.getUserId());
		invoiceImage.setUpdateUser(bladeUser.getUserId());
		invoiceImage.setCreateTime(new Date(System.currentTimeMillis()));
		invoiceImage.setUpdateTime(new Date(System.currentTimeMillis()));
		return R.status(imageService.uploadInvoicePdf(files, invoiceImage));
	}

	/**
	 * 批量上传发票
	 *
	 * @param files
	 * @return
	 */
	@PostMapping("/isExistInvoicePdf")
	public R isExistInvoicePdf(@RequestParam("files") MultipartFile[] files) {
		return imageService.isExistInvoicePdf(files);
	}
}
